package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.MemberActivateEntity;
import com.shycloud.mido.member.api.entity.MemberRole;
import com.shycloud.mido.member.mapper.MemberActivateMapper;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.MemberRoleMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 权限清除 controller
 *
 * @author nianhua.jiang
 * @date 2021/3/12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/permission")
@Api(tags = "清除权限")
@ApiIgnore
public class CleanPermissionController {

	/** 用户模块 mapper */
	private MemberMapper memberMapper;
	/** VIP 模块 mapper */
	private MemberRoleMapper roleMapper;
	/** 硬件权限 mapper */
	private MemberActivateMapper activateMapper;

	@GetMapping("cleanPermission")
	@ApiOperation("清除权限")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "username", value = "清除权限的用户名", paramType = "query", dataType = "String", required = true),
			@ApiImplicitParam(name = "zebraVip", value = "斑马VIP 0-未选择 1-已选择", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "zebraHardware", value = "斑马硬件权限 0-未选择 1-已选择", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "midoVip", value = "mido VIP 0-未选择 1-已选择", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "midoHardware", value = "mido 硬件权限 0-未选择 1-已选择", paramType = "query", dataType = "String")
	})
	public R<Boolean> cleanPermission(String username, String zebraVip, String zebraHardware, String midoVip, String midoHardware) {

		if ("1".equals(zebraVip)) {
			List<Member> members = memberMapper.selectList(new QueryWrapper<Member>().eq("username", username));
			for (Member member : members) {
				roleMapper.delete(new QueryWrapper<MemberRole>().eq("member_id", member.getId()));
			}
			return new R<>(true);
		}

		if ("1".equals(zebraHardware)) {
			List<Member> members = memberMapper.selectList(new QueryWrapper<Member>().eq("username", username));
			for (Member member : members) {
				activateMapper.delete(new QueryWrapper<MemberActivateEntity>().eq("member_id", member.getId()));
			}
			return new R<>(true);
		}

		//通过username获取memberId
		String midoMemberId = memberMapper.getMidoMemberId(username);

		if ("1".equals(midoVip)) {
			return new R<>(roleMapper.cleanMidoVip(midoMemberId) > 0);
		}

		if ("1".equals(midoHardware)) {
			return new R<>(activateMapper.cleanMidoHardware(midoMemberId) > 0);
		}

		return new R(true);

	}

}
